package oakclub.service;

import net.spy.memcached.MemcachedClient;
import net.spy.memcached.OperationTimeoutException;
import net.spy.memcached.internal.OperationFuture;
import oakclub.global.Enums;
import oakclub.global.Container;

public class MemcachedService
{
    private final MemcachedClient client = (MemcachedClient) Container.getComponent(Enums.Component.MEMCACHE_CLIENT);

    public enum Key
    {
        FB_TOKEN("fb.token", 1800), ONLINE("online", 90), IS_BLOCKED("is.blocked", 86400),
        FRIEND_IDS("friend.ids", 86400), FRIEND_INFOS("friend.infos", 86400),
        CHAT_NOTIFICATION("chat.notification", 600);

        private final String name;
        private final int expire;

        private Key(String name, int expire)
        {
            this.name = name;
            this.expire = expire;
        }

        public String getName()
        {
            return "oakclub.service_" + this.name;
        }

        public int getExpire()
        {
            return this.expire;
        }
    }

    public Object get(Key key, Object id) throws OperationTimeoutException, IllegalStateException
    {
        return this.client.get(key.getName() + '_' + id);
    }

    public OperationFuture<Boolean> set(Key key, Object id, Object val) throws IllegalStateException
    {
        return this.client.set(key.getName() + '_' + id, key.getExpire(), val);
    }

    public OperationFuture<Boolean> delete(Key key, Object id) throws IllegalStateException
    {
        return this.client.delete(key.getName() + '_' + id);
    }
}
